var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var app = getApp();
var wxAuthFail = true;
Page({
  data: {
    util: util,
    version: util.version,
    token: '',
    uid: 0,
    minHeight: 0,
    coin: 0.00,
    userinfo: {},
    cardlist:{},

    actionSheetHidden: true,

    isIphone_x: false,
    nav_bgColor: '#ffffff',
    isShow: false
  },
  onLoad: function (data) {
    this.getLoginInfo();
    var that = this;

    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getcardlist",
        "uid": that.data.uid
      },
      success: function (responseData) {
        console.log("卡券数据：", responseData.data);
        that.setData({
          cardlist: responseData.data
        });
      }
    });
    wx.getSystemInfo({
      success: function (res) {
        var isIphone_x = (res.model.indexOf('iPhone x') >= 0);
        that.setData({
          minHeight: res.windowHeight,
          isIphone_x: isIphone_x
        });
      }
    });
  },


  getLoginInfo: function () {
    var that = this;
    app.LoginUserInfo(function (res) {
      console.log("mine login:", res);
      wxAuthFail = res == -1;//用户拒绝授权
      if (wxAuthFail) {
        return;
      }
      that.setData({
        uid: res.uid,
        token: res.token
      });

      util.api({
        data: {
          "uid": res.uid,
          "publickey": util.publiccode,
          "ac": "userinfo"
        },
        success: function (responseData) {
          console.log(responseData.data);
          that.setData({
            userinfo: responseData.data
          });
        }
      });

      var params = {
        "uid": res.uid,
        "publickey": util.publiccode,
        "ac": "coininfo",
        "privatekey": utilMd5.hexMD5(res.uid + 'coininfo' + res.token)
      };
      console.log("余额参数：uid=" + res.uid + "; token = " + res.token, params);
      util.api({
        method: "post",
        data: params,
        success: function (responseData) {
          console.log("余额返回：", responseData);
          that.setData({
            coin: (responseData.data.coin || 0) / 100
          });
        }
      });

      util.api({
        data: {
          "publickey": util.publiccode,
          "ac": "wxcheck"
        },
        success: function (responseData) {
          that.setData({
            isShow: !responseData.data.isShow
          });
        }
      });
    });
  },

  //重新读取 登录用户信息
  reGetUserInfo: function (e) {
    var that = this;
    console.log("reGetUserInfo: ", e);
    if (e == 1) {
      that.getLoginInfo();
    }
  },

  actionSheetTap: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  actionSheetbindchange: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },

  goAtherPage: function (e) {
    if (wxAuthFail) {
      app.reAuthorize(this.reGetUserInfo);
    } else {
      var url = e.currentTarget.dataset.link;
      wx.navigateTo({ url: url })
    }
  },
  goAtherPageRedirect: function (e) {
    var url = e.currentTarget.dataset.link;
    wx.redirectTo({ url: url })
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    if (path) {
      wx.redirectTo({ url: path })
    }
  },


  newQuan: function (e) {
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getcard",
        "id": util.appid,
        "uid": that.data.uid,
        "cardId": e.currentTarget.dataset.cardid,
        //"cardId":"pD0RuwobZX56cuxKq90kSJXZVKLI",

      },
      success: function (responseData) {
        console.log(responseData);
        var signdata = responseData.data;
        var cext = '{"openid":"' + signdata.openid + '","nonce_str":"' + signdata.nonce_str + '","timestamp":"' + signdata.timestamp + '","signature":"' + signdata.signature + '"}';
        console.log(cext)
        wx.addCard({
          cardList: [{
            cardId: signdata.cardId,
            cardExt: '{"openid":"' + signdata.openid + '","nonce_str":"' + signdata.nonce_str + '","timestamp":"' + signdata.timestamp + '","signature":"' + signdata.signature + '"}'
          }], // 需要添加的卡券列表
          success: function (res) {
            console.log(res);
            var addinfo = res['cardList'][0]

            util.api({
              method: "post",
              data: {
                "publickey": util.publiccode,
                "ac": "saveusercard",
                "appid": util.appid,
                "uid": that.data.uid,
                "cardId": addinfo.cardId,
                "code": addinfo.code,
                "cartExt": addinfo.cartExt,
                "isSuccess": addinfo.isSuccess,
                "privatekey": utilMd5.hexMD5(that.data.uid + 'saveusercard' + that.data.token)

              },
              success: function (responseData) {
                console.log(responseData)
              }
            });
            console.log("你成功领取了奖品！");
          },
          cancel: function (res) {
            console.log(res)
            console.log('已取消')
          },
          fail: function (res) {
            console.log(res)
            console.log("失败")

          }
        });
      }
    });
  },

  showEWM: function (e) {
    var imgurl = e.currentTarget.dataset.imgsrc;
    console.log("图片链接：" + imgurl);
    wx.previewImage({
      current: imgurl,
      urls: [imgurl]
    })
  },
})